package com.lipy.java8study.thread;

import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import sun.nio.ch.ThreadPool;

import java.util.concurrent.*;

/**
 * @description:
 * @author: lipy
 * @create: 2020-07-16 19:40
 **/

public class ThreadDemo {
    public static void main(String[] args) {
//        test1();
        test2();
//        tese3();
//        tese4();
    }
    public static void tese3(){
        Object lock = null;
//                new Object();

        new Thread(() -> {
            synchronized (lock){
                if(lock != null){
                    try {
                        System.out.println("thread wait start");
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("lock thread");
            }
        }).start();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try{
            Object a= 1;
            String b = (String)a;

        }catch (Exception e){
            System.out.println("catch");
            throw e;
        }finally {
            synchronized (lock){
                System.out.println("finally lock");
                lock.notifyAll();
            }

            System.out.println("finall");
        }
        System.out.println(11);
    }
    public static void test1(){
        Thread thread = new Thread(()->{
            System.out.println(Thread.currentThread().getName());
        });
        thread.setName("123");
        thread.start();
    }
    public static void test2(){
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(), new CustomizableThreadFactory("prefix-"));
        for (int i = 0; i < 20; i++) {
            poolExecutor.execute(()->{
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
            });
        }

        poolExecutor.shutdown();
    }
    public static void tese4(){
        Object lock = new Object();

        synchronized (lock){
            if(lock != null){
                try {
                    System.out.println("thread wait start");
                    lock.wait(1000);
                    System.out.println("waiting over");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("lock thread");
        }
    }
}
